/*
 * @Author: zhangxuanjiu
 * @Date: 2017-08-10 14:59:35
 * @Last Modified by: zhangxuanjiu
 * @Last Modified time: 2017-09-29 11:14:56
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import initStore from './vuex/initStore'
import routes from './router/router'
import Vuex from 'vuex'
import util from './lib/util'
import './style/agent.less'
import Cookies from 'js-cookie'
import zip from 'lodash/zip'
import maxBy from 'lodash/maxBy'
import AwesomePicker from 'vue-awesome-picker';
const _ = { zip, maxBy }
import FastClick from 'fastclick'
window.FastClick = FastClick
window._ = _
window.Cookies = Cookies
// import _lodash from 'lodash'
// window._ = _lodash
window.util = util
Vue.use(Vuex)
Vue.use(VueRouter)
Vue.use(AwesomePicker)


const store = initStore()

const Root = {
  template: `
    <router-view></router-view>
  `
}

const router = new VueRouter({
  routes
})

const instance = new Vue({
  router,
  store, // 注入到所有子组件
  components: { Root }
})

// 代理等级转换过滤器
Vue.filter('agentLevel', function(value) {
  if (!value) { return '' }
  const strVal = value.toString()
  switch (strVal) {
    case '0':
      return '普通用户'
    case '1':
      return '总代理'
    case '2':
      return '一级代理'
    case '3':
      return '二级代理'
    default:
      return '暂未定义'
  }
})

function getQueryString (name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}
const token = getQueryString('token');
const areaCode = getQueryString('areaCode');
Cookies.set('areaCode', areaCode)
Cookies.set('token', token)
store.commit('changeToken', {token})
store.commit('changeAreaCode', {areaCode})
store.dispatch('getUserInfo')

instance.$mount('#root')

const btn_sound = document.createElement('audio');
btn_sound.setAttribute('src', 'http://c-c.jiahuagame.com/minigame/sound/down.mp3');
btn_sound.setAttribute('controls', 'controls');
btn_sound.setAttribute('hidden', 'true');
btn_sound.setAttribute('id', 'btn_sound');
document.body.appendChild(btn_sound);
window.btn_sound = btn_sound;

$(function() {
  FastClick.attach(document.body);
})